<template>
    <div class="hello">
        helloxxxxxxxxxxx
        openId = {{userId}}
        <label>
            <textarea rows="10" style="width: 90vw" v-model="sendViaMqtt"></textarea>
        </label>
        <button type="button" @click="sendMqttData">发送mqtt</button>
    </div>
</template>

<script>

    import {mapState} from "vuex";
    import {post, sleep} from "../js/utils";
    import {TenonWebsocket} from "../js/websocket";

    export default {
        name: 'HelloWorld',
        computed: {
            ...mapState(["userId"]),
        },
        async created() {
            console.log("helllllllllllllllllllllllllo")
            console.log("userId = ", this.userId)
            let ret = await post("/user/listGateways")
            console.log("ret = ", ret)
            await this.queryGatewayStatus()
            this.setUserGatewayKv()
            this.setUserDeviceKv()
        },
        data() {
            return {
                sendViaMqtt: "",
                msg: 'Welcome to Your Vue.js App22222222'
            }
        },

        methods: {
            sendMqttData() {
                if (!this.sendViaMqtt) {
                    alert("待发送的数据为空")
                    return
                }
                TenonWebsocket.sendData(this.sendViaMqtt)
            },
            async setUserGatewayKv() {
                await post("/user/setUserGatewayKv", {
                    gatewayId: "AABBCCDDEEFF",
                    "key": 123,
                    "value": "bbbbbb"
                })
            },

            async setUserDeviceKv() {
                await post("/user/setUserDeviceKv", {
                    devId: "16000001",
                    key: "aa",
                    // value: "bbbbbb"
                })
            },

            async queryGatewayStatus() {
                await sleep(3000)
                console.log("发送数据到mqtt上, t = ", new Date())
                TenonWebsocket.sendData(JSON.stringify({
                    "Cmd": "CmdToGateway",
                    "SubCmd": "queryGatewayStatus",
                    "GatewayId": "AABBCCDDEEFF",//此字段是为了方便与前端websocket通信使用
                }))
            }
        }

    }
</script>

<style scoped>

</style>
